import javax.swing.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author songx
 * @date 2019/7/3 22:59
 */
public class Main extends JFrame {

    public Main(JFrame e) {
        super("加班时长 计算小工具 @ songxiankun2008@126.com");
        mainPanel();
        setSize(800, 550);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setIconImage(new ImageIcon("src/main/resources/pig.png").getImage());
        setVisible(true);
        setLocationRelativeTo(null);
        repaint(1000);
        validate();
    }

    public static JTextField inPutTimeArea;
    public static JTextArea outPutContentArea;

    public void mainPanel() {

        getContentPane().setLayout(null);

        JLabel intPutLabel = new JLabel("请输入今天上班打卡的时间 (例如：9:30)");
        intPutLabel.setBounds(30, 20, 350, 30);
        intPutLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        getContentPane().add(intPutLabel);

        inPutTimeArea = new JTextField(1);
        inPutTimeArea.setBounds(30, 50, 250, 30);
        inPutTimeArea.setFont(new Font("华文行楷", Font.PLAIN, 18));
        inPutTimeArea.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    MouseClickSureEnter mouseClickSureEnter = new MouseClickSureEnter();
                    mouseClickSureEnter.mouseClicked(null);
                }
            }
        });
        getContentPane().add(inPutTimeArea);

        JButton sure = new JButton("确定");
        sure.setBounds(285, 50, 200, 30);
        sure.addMouseListener(new MouseClickSureEnter());
        sure.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    MouseClickSureEnter mouseClickSureEnter = new MouseClickSureEnter();
                    mouseClickSureEnter.mouseClicked(null);
                }
            }
        });

        getContentPane().add(sure);

        JButton clean = new JButton("重置");
        clean.setBounds(490, 50, 105, 30);
        clean.addMouseListener(new MouseClickCleanEnter());
        getContentPane().add(clean);

        JButton weather = new JButton("天气");
        weather.setBounds(600, 50, 70, 30);
        weather.addMouseListener(new MouseClickWeatherEnter());
        getContentPane().add(weather);

        JLabel icu996 = new JLabel();
        icu996.addMouseListener(new MouseClickIcuEnter());
        icu996.setBounds(675, 50, 80, 30);
        icu996.setIcon(new ImageIcon("src/main/resources/996.png"));
        getContentPane().add(icu996);

        outPutContentArea = new JTextArea();
        outPutContentArea.setBounds(30, 100, 730, 400);
        outPutContentArea.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        outPutContentArea.setLineWrap(true);
        outPutContentArea.setWrapStyleWord(true);
        getContentPane().add(outPutContentArea);

        JScrollPane scroll = new JScrollPane(outPutContentArea);
        scroll.setBounds(30, 100, 730, 400);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        getContentPane().add(scroll);

    }

    private static void playMusic() {
        try {
            URL cb;
            File f = new File("src/main/resources/sound.wav");
            cb = f.toURL();
            AudioClip aau;
            aau = Applet.newAudioClip(cb);
            aau.play();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        playMusic();
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main(null);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
